package org.jeecg.modules.demo.bid.service.impl;

import org.jeecg.modules.demo.bid.entity.ContractManagement;
import org.jeecg.modules.demo.bid.entity.ContactPerson;
import org.jeecg.modules.demo.bid.mapper.ContactPersonMapper;
import org.jeecg.modules.demo.bid.mapper.ContractManagementMapper;
import org.jeecg.modules.demo.bid.service.IContractManagementService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 合同管理
 * @Author: jeecg-boot
 * @Date:   2021-11-28
 * @Version: V1.0
 */
@Service
public class ContractManagementServiceImpl extends ServiceImpl<ContractManagementMapper, ContractManagement> implements IContractManagementService {

	@Autowired
	private ContractManagementMapper contractManagementMapper;
	@Autowired
	private ContactPersonMapper contactPersonMapper;
	
	@Override
	@Transactional
	public void saveMain(ContractManagement contractManagement, List<ContactPerson> contactPersonList) {
		contractManagementMapper.insert(contractManagement);
		if(contactPersonList!=null && contactPersonList.size()>0) {
			for(ContactPerson entity:contactPersonList) {
				//外键设置
				entity.setContactId(contractManagement.getId());
				contactPersonMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(ContractManagement contractManagement,List<ContactPerson> contactPersonList) {
		contractManagementMapper.updateById(contractManagement);
		
		//1.先删除子表数据
		contactPersonMapper.deleteByMainId(contractManagement.getId());
		
		//2.子表数据重新插入
		if(contactPersonList!=null && contactPersonList.size()>0) {
			for(ContactPerson entity:contactPersonList) {
				//外键设置
				entity.setContactId(contractManagement.getId());
				contactPersonMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		contactPersonMapper.deleteByMainId(id);
		contractManagementMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			contactPersonMapper.deleteByMainId(id.toString());
			contractManagementMapper.deleteById(id);
		}
	}
	
}
